package service

import (
	"ksd-social-api/modules/note/mapper"
	"ksd-social-api/modules/note/model"
	"ksd-social-api/modules/note/model/context"
)

/**
 * 用户收藏
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type NoteUserFavService struct {
}

var noteUserFavMapper = mapper.NoteUserFavMapper{}

/**
 * 收藏
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserFavService) SaveCancelUserFav(ctx context.FavLikeContext) int64 {
	isFocus := noteUserFavMapper.CountNoteUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
	// 收藏
	if isFocus == -1 {
		var noteUserFav model.NoteUserFav
		noteUserFav.UserId = ctx.UserId
		noteUserFav.SystemId = ctx.SystemId
		noteUserFav.Opid = ctx.Opid.Uint64()
		noteUserFav.Avatar = ctx.Avatar
		noteUserFav.Nickname = ctx.Nickname
		noteUserFav.Username = ctx.Username
		noteUserFav.SystemName = ctx.SystemName
		noteUserFav.Uuid = ctx.Uuid
		noteUserFav.IsFocus = 1
		noteUserFavMapper.SaveNoteUserFav(ctx.Opid.Uint64(), noteUserFav)
		return 1
	}

	// 再收藏
	if isFocus == 0 {
		noteUserFavMapper.RecoverNoteUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
		return 1
	}

	// 取消收藏
	if isFocus == 1 {
		noteUserFavMapper.CancelNoteUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
		return 0
	}

	return 0
}

/**
 * 查询是否收藏
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *NoteUserFavService) GetUserFav(userId uint64, focusId uint64, systemId uint64) int64 {
	return noteUserFavMapper.CountNoteUserFav(userId, focusId, systemId)
}

/**
 * 删除收藏
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserFavService) DelUserFav(ctx context.FavLikeDelContext) bool {
	return noteUserFavMapper.DelNoteUserFav(ctx.Id.Uint64(), ctx.UserId, ctx.NoteId.Uint64(), ctx.SystemId)
}
